﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MvvmEx.Controls.DateShow
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:MvvmEx.Controls.DateShow"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:MvvmEx.Controls.DateShow;assembly=MvvmEx.Controls.DateShow"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误: 
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:DateShowItem/>
    ///
    /// </summary>
    public class DateShowItem : Button
    {

        #region 字段

        private DateShow _dateShow;

        #endregion

        #region 依赖属性

        #region 是否今天

        public bool IsToDay
        {
            get { return (bool)GetValue(IsToDayProperty); }
            set { SetValue(IsToDayProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsToDay.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsToDayProperty =
            DependencyProperty.Register("IsToDay", typeof(bool), typeof(DateShowItem), new PropertyMetadata(false));

        #endregion

        #region 行
        public int Row
        {
            get { return (int)GetValue(RowProperty); }
            set { SetValue(RowProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Row.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RowProperty =
            DependencyProperty.Register("Row", typeof(int), typeof(DateShowItem), new PropertyMetadata(0, (s, e) =>
            {
                var obj = s as DateShowItem;

                if (obj == null) return;

                Grid.SetRow(obj, (int)e.NewValue);
            }));
        #endregion

        #region 列

        public int Column
        {
            get { return (int)GetValue(ColumnProperty); }
            set { SetValue(ColumnProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Column.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColumnProperty =
            DependencyProperty.Register("Column", typeof(int), typeof(DateShowItem), new PropertyMetadata(0,
                (s, e) =>
                {
                    var obj = s as DateShowItem;

                    if (obj == null) return;

                    Grid.SetColumn(obj, (int)e.NewValue);
                }));

        #endregion

        #region 天

        public int Day
        {
            get { return (int)GetValue(DayProperty); }
            set { SetValue(DayProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Day.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DayProperty =
            DependencyProperty.Register("Day", typeof(int), typeof(DateShowItem), new PropertyMetadata(0));

        #endregion

        #region 日期
        public DateTime Date
        {
            get { return (DateTime)GetValue(DateProperty); }
            set { SetValue(DateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Date.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DateProperty =
            DependencyProperty.Register("Date", typeof(DateTime), typeof(Button), new PropertyMetadata(DateTime.MinValue,
                (s, e) =>
                {
                    var obj = s as DateShowItem;

                    if (obj != null)
                    {
                        obj.Day = ((DateTime)e.NewValue).Day;
                    }
                }));
        #endregion

        #region 是否选中
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(DateShowItem), new PropertyMetadata(false));
        #endregion

        #endregion

        #region 构造
        static DateShowItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DateShowItem), new FrameworkPropertyMetadata(typeof(DateShowItem)));
        }

        public DateShowItem(DateShow dateShow)
        {
            _dateShow = dateShow;

            IsEnabled = false;

            this.Click += DateShowItem_Click;
        }


        #endregion

        #region 私有方法

        #region 单击事件
        private void DateShowItem_Click(object sender, RoutedEventArgs e)
        {
            _dateShow.OnSelectedDateChanged(Date);
        }
        #endregion

        #endregion
    }
}
